Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegexArrayShapeMatcher - More precise non-empty-string and numeric-string #3249

Merged
merged 18 commits into from
Jul 17, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Jul 16, 2024

based on the regex ast we try to detect more precise string types, like non-empty-string and numeric-string.

I intentionally left out constant-string types for now to reduce complexity and learn from real world use-cases in the wild.

@staabm
Copy link
Contributor Author

staabm commented Jul 17, 2024

//cc @Seldaek would be great to have another pair of eyeballs on the test-expectations

@staabm staabm marked this pull request as ready for review July 17, 2024 09:27
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@ondrejmirtes ondrejmirtes merged commit 3d9b04c into phpstan:1.11.x Jul 17, 2024
441 of 455 checks passed
@ondrejmirtes
Copy link
Member

Thank you.

1 similar comment
@ondrejmirtes
Copy link
Member

Thank you.

@ondrejmirtes
Copy link
Member

Please send update to phpstan-nette :)

@staabm staabm deleted the preg-numeric branch July 17, 2024 14:26
Comment on lines 24 to +25
if (preg_match('/Price: (£|€)?\d+/', $s, $matches, PREG_UNMATCHED_AS_NULL)) {
assertType('array{0: string, 1?: string}', $matches);
assertType('array{0: string, 1?: non-empty-string}', $matches);
Copy link
Contributor

@Seldaek Seldaek Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given unmatched as null this should be 1: non-empty-string|null IMO.

preg_match('/Price: (£|€)?\d+/', 'Price: 3', $matches, PREG_UNMATCHED_AS_NULL) for ex yields:

array (size=2)
  0 => string 'Price: 3' (length=8)
  1 => null

Copy link
Contributor Author

@staabm staabm Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is the php72 variant, which does not "correctly support" nulls in the PREG_UNMATCHED_AS_NULL case (because php-src behaves differently).

see https://3v4l.org/No3Tf#v7.3.26

there is the identical test but for php7.4+ with the expectations you just shared:
https://github.com/phpstan/phpstan-src/pull/3249/files#diff-587efc95b2825fa3f959191a2c1dae92edda5ad901653ae174e58bc723152fa9R25

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry I missed that 🤦🏻‍♂️

@Seldaek
Copy link
Contributor

Seldaek commented Jul 17, 2024

Ok this is amazing :D I can't wait to try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants